﻿@inherits BaseNav
@attribute [NavPlugin("用户", "user", Category = "组件")]

<AntDropdown Context="Context" Model="model">
    <div style="display:flex;align-items:center;margin-top:3px;">
        <Avatar Src="@AvatarUrl" />
        <span>@CurrentUser?.Name</span>
    </div>
</AntDropdown>

@code {
    private DropdownModel model = new();
    private string AvatarUrl => CurrentUser?.AvatarUrl ?? "img/face1.png";
    [CascadingParameter] private TopNavbar Topbar { get; set; }

    protected override async Task OnInitAsync()
    {
        await base.OnInitAsync();
        model.Items = new List<ActionInfo>();
        model.Items.Add(new ActionInfo { Id = "profile", Name = Language["Nav.Profile"], Icon = "user", Url = "/profile" });
        model.Items.Add(new ActionInfo { Id = "logout", Name = Language["Nav.Exit"], Icon = "poweroff" });
        model.OnItemClick = OnItemClickAsync;
    }

    private Task OnItemClickAsync(ActionInfo item)
    {
        if (!string.IsNullOrWhiteSpace(item.Url))
            Navigation?.NavigateTo(item);
        else
            Topbar?.OnMenuClick?.Invoke(item.Id);
        return Task.CompletedTask;
    }
}